package com.util;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class SessionCheckFilter
 */
@WebFilter(urlPatterns={"/*"})
public class SessionCheckFilter implements Filter {
	private FilterConfig config;

    public SessionCheckFilter() {
      
    }

	public void destroy() {
		this.config=null;
	}

	public void init(FilterConfig fConfig) throws ServletException {
		this.config=fConfig;
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpSession session=((HttpServletRequest)request).getSession();
		String requestPath=((HttpServletRequest)request).getServletPath();
		String func=(String)request.getParameter("func");
		if(session.getAttribute("loginUser")!=null
				|| ("/message/manager".equals(requestPath) && func==null)
				|| "/user/login.jsp".equals(requestPath)
				|| "login".equals(func)
				|| "/user/addUser.jsp".equals(requestPath)
				|| "addUser".equals(func)){
			chain.doFilter(request, response);
		}else{
			request.setAttribute("message","您还未登陆，请先登录！");
			request.getRequestDispatcher("/user/login.jsp").forward(request, response);
		}
		
	}

}
